<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1
        :class="!openMenu ? 'site-navbar__brand' : 'openMenuFontSize'"
        @click="$router.push({ name: 'home' })"
      >
        <a class="site-navbar__brand-lg" href="javascript:;">
          <!-- {{nickName == '浩琪换电' ? "浩琪换电" : nickName == '小熊唤电' ? "小熊唤电" : "云禾换电"}} -->
          {{ remark }}
        </a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <el-menu class="site-navbar__menu" mode="horizontal">
        <!--        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">-->
        <!--          <icon-svg name="zhedie"></icon-svg>-->
        <!--        </el-menu-item>-->
      </el-menu>
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal"
      >
        <!-- <el-menu-item index="1" @click="$router.push({ name: 'theme' })">
          <template slot="title">
            <el-badge value="new">
              <icon-svg name="shezhi" class="el-icon-setting"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
        <el-menu-item index="2">
          <el-badge value="hot">
            <a href="https://www.renren.io/" target="_blank">官方社区</a>
          </el-badge>
        </el-menu-item>
        <el-submenu index="3">
          <template slot="title">Git源码</template>
          <el-menu-item index="2-1"><a href="https://github.com/renrenio/renren-fast-vue" target="_blank">前端</a></el-menu-item>
          <el-menu-item index="2-2"><a href="https://gitee.com/renrenio/renren-fast" target="_blank">后台</a></el-menu-item>
          <el-menu-item index="2-3"><a href="https://gitee.com/renrenio/renren-generator" target="_blank">代码生成器</a></el-menu-item>
        </el-submenu>-->
        <el-menu-item class="site-navbar__avatar" index="3">
          <!--          <el-dropdown :show-timeout="0" placement="bottom">-->
          <span class="el-dropdown-link">
            <!--              <img src="~@/assets/img/avatar.png" :alt="userName">-->
            {{ user.roleName }}-{{ user.nickName }}
          </span>
          <!--            <el-dropdown-menu slot="dropdown">-->
          <!--              <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>-->
          <!--              <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>-->
          <!--            </el-dropdown-menu>-->
          <!--          </el-dropdown>-->
        </el-menu-item>

        <el-menu-item index="4" @click.native="logoutHandle()">
          <span>Logout</span>
          <!-- <el-badge value="hot">
				    <span>Logout</span>
          </el-badge>-->
        </el-menu-item>
        <el-menu-item index="5" @click.native="setPassWordFn()"
          >Change Password</el-menu-item
        >
        <el-menu-item>
          <i
            @click="openMenuFn"
            class="el-icon-menu"
            :class="!openMenu ? 'greenClass' : 'blueClass'"
          ></i>
          <i
            @click="openColorFn"
            :class="openColorMenu ? 'el-icon-sunny' : 'el-icon-moon-night'"
          ></i>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password
      v-if="updatePassowrdVisible"
      ref="updatePassowrd"
    ></update-password>
    <el-dialog
      width="460px"
      title="Change Password"
      :visible.sync="renewalShow"
      :fullscreen="false"
      :modal="false"
    >
      <el-form :model="renewalData" :rules="renewalRules" ref="ruleForm">
        <el-form-item
          label="Old Password"
          label-width="130px"
          prop="oldPassword"
        >
          <el-input type="text" v-model="renewalData.oldPassword"></el-input>
        </el-form-item>
        <el-form-item
          label="New Password"
          label-width="130px"
          prop="newPassword"
        >
          <el-input type="text" v-model="renewalData.newPassword"></el-input>
        </el-form-item>
        <el-form-item
          label="Enter Again New PassWord"
          label-width="130px"
          prop="newPassword1"
        >
          <el-input type="text" v-model="renewalData.newPassword1"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="renewalShow = false">Cancel</el-button>
        <el-button type="primary" @click="affirmAgentFn('ruleForm')"
          >Confirm</el-button
        >
      </div>
    </el-dialog>
  </nav>
</template>

<script>
import UpdatePassword from "./main-navbar-update-password";
import { clearLoginInfo } from "@/utils";
export default {
  data() {
    return {
      // 修改密码   start
      renewalShow: false,
      renewalData: {
        oldPassword: "",
        newPassword: "",
        newPassword1: ""
      },
      renewalRules: {
        oldPassword: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        newPassword1: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ]
      },
      // 修改密码  end
      openMenu: false,
      openColorMenu: false, //false为dark黑色，true为light浅色
      updatePassowrdVisible: false,

      programName: "",

      user: {},

      url: process.env.VUE_APP_URL
    };
  },
  components: {
    UpdatePassword
  },
  // watch: {
  //   openMenu: {
  //     handler(newValue, oldValue) {
  //       console.log('新的：', newValue, '旧的：', oldValue)
  //     },
  //     deep: true  // 深度监听
  //   }
  // },
  computed: {
    navbarLayoutType: {
      get() {
        return this.$store.state.common.navbarLayoutType;
      }
    },
    sidebarFold: {
      get() {
        return this.$store.state.common.sidebarFold;
      },
      set(val) {
        this.$store.commit("common/updateSidebarFold", val);
      }
    },

    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      }
    },
    userName: {
      get() {
        return sessionStorage.username;
      }
    },
    roleName() {
      return sessionStorage.roleName;
    },
    name() {
      return sessionStorage.name;
    },
    remark: {
      get() {
        return sessionStorage.getItem("remark");
      }
    },
    nickName: {
      get() {
        return sessionStorage.getItem("nickName");
      }
    }
  },
  created() {
    this.getData();
  },
  methods: {
    openMenuFn() {
      this.openMenu = !this.openMenu;
      this.$store.commit("common/updateSidebarFold", this.openMenu);
      console.log(4444, this.$store.state.common.sidebarFold);
    },
    openColorFn() {
      this.openColorMenu = !this.openColorMenu;
      if (!this.openColorMenu) {
        this.$store.commit("common/updateSidebarLayoutSkin", "dark");
      } else {
        this.$store.commit("common/updateSidebarLayoutSkin", "light");
      }
    },

    async getData() {
      var res = await this.apis.getInfo({});
      var data = res.user || {};
      this.user = data;
      // this.programName=data.programName||'';
      // document.title=data.programName||''
    },
    // 修改密码
    updatePasswordHandle() {
      this.updatePassowrdVisible = true;
      this.$nextTick(() => {
        this.$refs.updatePassowrd.init();
      });
    },
    // 退出
    logoutHandle() {
      this.$confirm(`Confirm Logout?`, "Hint", {
        confirmButtonText: "Confirm",
        cancelButtonText: "Cancel",
        type: "warning"
      })
        .then(() => {
          // console.warn('main-navbar.vue重新加入代码')
          // this.$http.get("/sysUser/sys/exitlogin").then(res => {
          //   if (res.code === 200) {
          //     clearLoginInfo()
          //     this.$router.push({ name: 'login' })
          //   }
          // })

          sessionStorage.role = "";
          clearLoginInfo();
          sessionStorage.mainTabs_text = "";
          this.$store.commit("common/updateMainTabs", []);
          this.$router.push({
            path: "/login"
          });
        })
        .catch(() => {});
    },
    // 修改密码
    setPassWordFn() {
      this.renewalShow = true;
    },
    // 修改密码确认按钮
    affirmAgentFn(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          if (this.renewalData.newPassword !== this.renewalData.newPassword1) {
            this.$message({
              type: "error",
              message: "您两次输入的新密码不相同，请仔细核对"
            });
            return;
          }
          let parm = {
            oldPassword: this.renewalData.oldPassword,
            newPassword: this.renewalData.newPassword
          };
          const res = await this.apis.userSettingPwd(parm);
          this.$message({
            type: res.code == 200 ? "success" : "error",
            message:
              res.code == 200
                ? "修改密码Success，即将退出该账号，请用新密码重新登录"
                : res.msg
          });
          if (res.code == 200) {
            this.renewalShow = false;
            setTimeout(() => {
              sessionStorage.role = "";
              clearLoginInfo();
              sessionStorage.mainTabs_text = "";
              this.$store.commit("common/updateMainTabs", []);
              this.$router.push({
                path: "/login"
              });
            }, 3000);
          }
        }
      });
    }
  }
};
</script>

<style>
.greenClass {
  color: rgb(84, 109, 224) !important;
}

.blueClass {
  color: #999 !important;
}

.openMenuFontSize {
  font-size: 13px !important;
  display: table-cell;
  vertical-align: middle;
  width: 230px;
  height: 50px;
  margin: 0;
  line-height: 50px;
  text-align: center;
  text-transform: uppercase;
  white-space: nowrap;
  color: #fff;
}
</style>
